--
-- Author: shunguo.chen
-- Date: 2018-03-07 16:46:25
--
-- ChessMiniRender 谜棋小地图

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_GOOD = cc.c3b(9,148,75)
local COLOR_BAD = cc.c3b(255,255,255)

-- data = ChessCell/nil
function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChessMiniNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._ui.bg_b:setVisible(false)
	self._ui.bg_r:setVisible(false)

	self:refresh()
end

function ClassRef:refresh()
	self._ui.icon:setVisible(self._data ~= nil)
	if self._data then
		local data = self._data
		local isAble = data:isTriggerAble(UD:getUid())

		self._ui.icon:setVisible(isAble)
		self._ui.num_1:setVisible(false)
		self._ui.num_2:setVisible(false)

		if isAble then
			self._ui.icon:loadTexture(data:getIcon())
			local tp, val = data:getShowTypeValue()
			if tp == "2" or tp == "3" then
				local isBad = data:isBad()
				local color = isBad and COLOR_BAD or COLOR_GOOD
				if tp == "2" then
					self._ui.num_1:setTextColor(color)
					self._ui.num_1:setVisible(true)
					self._ui.num_1:setString(val)
				else
					self._ui.num_2:setTextColor(color)
					self._ui.num_2:setVisible(true)
					self._ui.num_2:setString(val)
				end
			elseif tp == "13" then
				-- buff类型
				local buffTp, buffVal = data:getShowBuffTypeValue()
				if buffTp == "1" then
					-- 下回合投掷固定点数
					local isBad = data:isBad()
					local color = isBad and COLOR_BAD or COLOR_GOOD
					self._ui.num_2:setTextColor(color)
					self._ui.num_2:setVisible(true)
					self._ui.num_2:setString(buffVal)
				end
			end

			self._ui.dot:setVisible(data:getTarget() == "1")
		end
	else
		self._ui.num_1:setVisible(false)
		self._ui.num_2:setVisible(false)
		self._ui.icon:setVisible(false)
	end
end

function ClassRef:setLight(b, isMe)
	local bg = isMe and self._ui.bg_b or self._ui.bg_r
	local visible = bg:isVisible()
	if visible ~= b then
		bg:setVisible(b)
		if b then
			self._ui.icon:setOpacity(255 * 0.9)
		else
			local fade = cca.fadeIn(0.5)
			self._ui.icon:runAction(fade)
		end
	end
end

function ClassRef:setTouchTip(tip)
	-- click tip
	display.uiAddClick(self._ui.Panel, function()
			if self._data and self._data:isTriggerAble(UD:getUid()) then
				local worldPos = self:convertToWorldSpace(cc.p(0, -30))
				local nodePos = tip:getParent():convertToNodeSpace(worldPos)
				tip:setPosition(nodePos)
				tip:setData(self._data:getTipMsg())
				tip:show()
			end
		end)

	-- touch tip
	-- self._ui.Panel:addTouchEventListener(function(sender, eventType)
	-- 	if self._data and self._data:isTriggerAble(UD:getUid()) then
	-- 		if eventType == ccui.TouchEventType.began then
	-- 			local worldPos = self:convertToWorldSpace(cc.p(0, -30))
	-- 			local nodePos = tip:getParent():convertToNodeSpace(worldPos)
	-- 			tip:setPosition(nodePos)
	-- 			tip:setData(self._data:getTipMsg())
	-- 			tip:show()
	-- 		else
	-- 			tip:hide()
	-- 		end
	-- 	end
	-- end)
end

function ClassRef:setIndex(idx)
	self._idx = idx
end

function ClassRef:getIndex()
	return self._idx
end

return ClassRef
